package com.whfc.ms.api.base.service.impl;

import com.whfc.base.dto.SceneDTO;
import com.whfc.base.service.AppSceneService;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.util.RandomUtil;
import com.whfc.entity.dto.UploadImgDTO;
import com.whfc.ms.api.base.service.MsAppSceneService;
import org.apache.commons.io.FilenameUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClasssName MsAppSceneServiceImpl
 * @Description 现场模拟
 * @Author hw
 * @Date 2021/3/25 16:05
 * @Version 1.0
 */
@Service
public class MsAppSceneServiceImpl implements MsAppSceneService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = AppSceneService.class, version = "1.0.0")
    private AppSceneService appSceneService;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;

    @Override
    public void add(SceneDTO param) {
        appSceneService.add(param);
    }

    @Override
    public void edit(SceneDTO param) {
        appSceneService.edit(param);
    }

    @Override
    public void del(Integer id) {
        appSceneService.del(id);
    }

    @Override
    public SceneDTO details(Integer deptId) {
        return appSceneService.details(deptId);
    }

    @Override
    public UploadImgDTO uploadImg(MultipartFile file) {
        UploadImgDTO uploadImgDTO = new UploadImgDTO();
        try {
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            String fileName = RandomUtil.getRandomFileName() + "." + extension;
            String upload = fileHandler.upload(filePathConfig.getFilePath("scene/img/") + fileName, file.getInputStream());
            uploadImgDTO.setUrl(upload);
        } catch (IOException e) {
            logger.error("文件上传失败", e);
        }
        return uploadImgDTO;
    }
}
